import *  as Cesium from 'cesium';

export function addJsonData(viewer) {

  Promise.all([
    Cesium.GeoJsonDataSource.load('/GeoJSON/gd_area.geojson', {
      clampToGround: true, // 不贴地
      fill: Cesium.Color.YELLOW.withAlpha(0.8),
      stroke: Cesium.Color.BLACK,
    }),
    Cesium.GeoJsonDataSource.load('/GeoJSON/gd_point.geojson', {
      clampToGround: true, // 贴地
      markerSize: 8, // 禁用浮标
      point: {
        pixelSize: 8, // 设置点大小
        color: Cesium.Color.RED,
        outlineColor: Cesium.Color.WHITE,
        outlineWidth: 1
      }
    })
  ]).then(async ([area, point]) => {
    viewer.dataSources.add(area);
    viewer.dataSources.add(point);


    const entities = point.entities.values
    for (const entity of entities) {
      const position = entity.position.getValue(Cesium.JulianDate.now()) //JulianDate是时间对象
      const heightAboveTerrain = 2805; // 调整这个值以确保模型可见
      const cartographic = Cesium.Cartographic.fromCartesian(position) // 转为经纬度高度
      cartographic.height += heightAboveTerrain // 高度调整
      const newPosition = Cesium.Cartesian3.fromRadians(cartographic.longitude, cartographic.latitude, cartographic.height) // 转为笛卡尔坐标

      const model = await Cesium.Model.fromGltfAsync({
        url: '/model/SolarEngine/simple_solar_station_1.glb',
        modelMatrix: Cesium.Matrix4.fromTranslation(newPosition), // 位置
        // modelMatrix:Cesium.Matrix4.fromRotationY(Cesium.Math.toRadians(45)), // 旋转
        scale: 10.0, // 缩放
        minimumPixelSize: 32, // 确保小尺寸可见
        maximumScale: 200,   // 防止过大
        show: true           // 确保显示
      })
      viewer.scene.primitives.add(model)
    }

});
}